package queue

import "errors"

type tQueueIterator struct {
	queue *tArrayQueue
	pos int
	version int64
}

var gConcurrentModificationError = errors.New("concurrent modification error")
var gNoMoreElementsError = errors.New("no more elements")

func newArrayQueueIterator(queue *tArrayQueue) IListIterator {
	return &tQueueIterator{
		queue: queue,
		pos : queue.rindex,
		version: queue.version,
	}
}

func (me *tQueueIterator) More() bool {
	q := me.queue
	if q == nil {
		return false
	}

	if q.version != me.version {
		return false
	}

	return me.pos < q.windex
}

func (me *tQueueIterator) Next() (error, interface{}) {
	q := me.queue
	if q == nil {
		return gEmptyQueueError, nil
	}

	if q.version != me.version {
		return gConcurrentModificationError, nil
	}

	if me.pos >= q.windex {
		return gNoMoreElementsError, nil
	}

	it := q.items[me.pos]
	me.pos++
	return nil, it
}